package framework.mybatis.annotation2.mapper;

import framework.mybatis.annotation2.entity.User;
import framework.mybatis.annotation2.entity.UserVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 通过 Java config 配置 mybatis
 *
 * @author ME
 * @date 2021/4/29
 */
public interface UserMapper {

    @Select("<script>SELECT * FROM sys_user " +
            "        <where>" +
            "            <if test=\"id != null\">" +
            "                `id` = #{id}" +
            "            </if>" +
            "            <if test=\"name != null\">" +
            "                AND `name` = #{name}" +
            "            </if>" +
            "            <if test=\"age != null\">" +
            "                AND `age` = #{age}" +
            "            </if>" +
            "        </where></script>")
    User getUser(User user);

    @Select("<script>select * from sys_user " +
            "        <where>" +
            "            <if test=\"id != null\">" +
            "                `id` = #{id}" +
            "            </if>" +
            "            <if test=\"name != null\">" +
            "                AND `name` = #{name}" +
            "            </if>" +
            "            <if test=\"age != null\">" +
            "                AND `age` = #{age}" +
            "            </if>" +
            "        </where></script>")
    UserVO getUserVO(User user);

    @Insert("insert into sys_user(id,`name`, age) values(#{id}, #{name}, #{age})")
    int addUser(User user);

    @Update("update sys_user set `name`=#{name}, age=#{age} where id = #{id}")
    int updateUser(User user);

    @Delete("delete from sys_user where id = #{id}")
    int delUser(User user);

}
